package com.cake.liulanxiangzhu.service.impl;

import com.cake.liulanxiangzhu.exception.ServiceException;
import com.cake.liulanxiangzhu.mapper.AdminCakeskuMapper;
import com.cake.liulanxiangzhu.pojo.entity.AdminCakesku;
import com.cake.liulanxiangzhu.pojo.entity.OmsCart;
import com.cake.liulanxiangzhu.pojo.dto.OmsCartDTO;
import com.cake.liulanxiangzhu.pojo.dto.OmsCartUpdateDTO;
import com.cake.liulanxiangzhu.pojo.vo.OmsCartVO;
import com.cake.liulanxiangzhu.mapper.OmsCartMapper;
import com.cake.liulanxiangzhu.service.OmsCartService;
import com.cake.liulanxiangzhu.utils.DataUseUtil;
import com.cake.liulanxiangzhu.utils.LoginUtil;
import com.cake.liulanxiangzhu.web.JsonPage;
import com.cake.liulanxiangzhu.web.ServiceCode;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * 购物车信息表(OmsCart)表服务实现类
 *
 * @author dinghao
 * @since 2023-03-29 09:18:13
 */
@Service
public class OmsCartServiceImpl implements OmsCartService {
    @Autowired
    private OmsCartMapper omsCartMapper;

    @Autowired
    private LoginUtil loginUtil;

    @Autowired
    private AdminCakeskuMapper adminCakeskuMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param cartId 主键
     * @return 实例对象
     */
    @Override
    public OmsCartVO queryById(Integer cartId) {
        return this.omsCartMapper.queryById(cartId);
    }

    /**
     * 用户查询购物车
     *
     * @param request 主键
     * @return 实例对象
     */
    @Override
    public JsonPage<OmsCartVO> queryByPage(HttpServletRequest request, Integer page , Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        String token = request.getHeader("Token");
        if (DataUseUtil.isBlank(token)) {
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST, "用户未登录");
        }
        String userId = loginUtil.parser(token).getId();
        List<OmsCartVO> omsCartVOS = omsCartMapper.queryByUserId(Integer.parseInt(userId));
        return JsonPage.restPage(new PageInfo<>(omsCartVOS));
    }

    @Override
    public JsonPage<OmsCartVO> queryAdminByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        OmsCart omsCart = new OmsCart();
        List<OmsCartVO> omsCartVOS = omsCartMapper.queryAllByLimit(omsCart);
        return JsonPage.restPage(new PageInfo<>(omsCartVOS));
    }

    /**
     * 新增数据
     *
     * @param request
     * @param omsCartDto 实例对象
     * @return 实例对象
     */
    @Override
    public void insert(HttpServletRequest request, OmsCartDTO omsCartDto) {
        String token = request.getHeader("Token");
        if (DataUseUtil.isBlank(token)) {
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST, "用户未登录");
        }
        String userId = loginUtil.parser(token).getId();
        //String userId = "1";
        OmsCart omsCart = new OmsCart();
        BeanUtils.copyProperties(omsCartDto,omsCart);
        omsCart.setUserId(Integer.parseInt(userId));
        AdminCakesku adminCakesku = adminCakeskuMapper.selectById(omsCart.getSkuId());
        omsCart.setTittle(adminCakesku.getSkuName());
        omsCart.setCreatTime(new Date());
        omsCart.setUpdateTime(new Date());
        omsCartMapper.insert(omsCart);
    }

    @Override
    public void insertBatchCart(List<OmsCartDTO> omsCartDTOS) {


    }

    /**
     * 修改数据
     *
     * @param omsCartUpdateDto 实例对象
     * @return 实例对象
     */
    @Override
    public void update(OmsCartUpdateDTO omsCartUpdateDto) {
        OmsCart omsCart = new OmsCart();
        BeanUtils.copyProperties(omsCartUpdateDto,omsCart);
        omsCart.setUpdateTime(new Date());
        omsCartMapper.update(omsCart);
    }

    /**
     * 通过主键删除数据
     *
     * @param cartId 主键
     * @return 是否成功
     */
    @Override
    public void deleteById(Integer cartId) {
        omsCartMapper.deleteById(cartId);
    }

    @Override
    public void deleteByIdBatch(List<Integer> cartIds) {
        omsCartMapper.deleteByIdBatch(cartIds);
    }

}
